﻿using System;
using wojilu.Apps.Qycms.Domain;
using wojilu.Members.Interface;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Sites.Service;
using wojilu.Members.Users.Domain;
using wojilu.Web.Controller.Common;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.Admin.Qycms
{
    public class SiteController : BaseController
    {
        public virtual IAdminLogService<SiteLog> logService { get; set; }

        public SiteController()
        {
            logService = new SiteLogService();
        }

        private void setpublic()
        {
            User user = ctx.viewer.obj as User;
            var modelPowerGroup = PowerGroup.findById(user.GroupId);
            bind("powergroup", modelPowerGroup);
        }

        [HttpGet, HttpPost]
        public virtual void SiteList()
        {
            SetPublic();
            setpublic();
            var condition = String.Empty;
            DataPage<site> list = null;
            Int32 maxperpage = ctx.PostInt("MaxPerPage");
            Int32 page_id = ctx.PostInt("page_id") + 1;
            String limitkey = ctx.Get("limitkey");
            String limitclass = ctx.Get("limitclass");

            if (strUtil.IsNullOrEmpty(limitkey)) limitkey = "id";
            if (strUtil.IsNullOrEmpty(limitclass)) limitclass = "desc";
            condition += " order by " + limitkey + " " + limitclass;
            CurrentRequest.setCurrentPage(page_id);
            list = site.findPage(condition, maxperpage);
            if (ctx.web.postValueAll().Count == 0)
            {
                ctx.web.ResponseWrite(list.RecordCount.ToString());
                ctx.web.ResponseEnd();
            }
            bind("list", list.Results);
        }

        [HttpGet]
        public virtual void SiteEdit(Int64 id)
        {
            SetPublic();
            setpublic();
            target(SiteEditDo, id);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            set("tab", "true");
            site s = site.findById(id);
            bind("read", s);
        }

        [HttpPost]
        public virtual void SiteEditDo(Int64 id)
        {
            site s = site.findById(id);
            s.sitename = ctx.Post("sitename");
            s.sitehttp = ctx.Post("sitehttp");
            s.sitedir = ctx.Post("sitedir");
            s.sitecode = ctx.Post("sitecode");
            s.siteip = ctx.Post("siteip");
            s.adminusername = ctx.Post("adminusername");
            s.addtime = DateTime.Now;
            Result result = s.update();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("sitemain_edit_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("sitemain_edit_log") + lang("log_extra_ok"), "{Id:" + s.Id + ", Name:'" + s.sitename + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void SiteAdd()
        {
            SetPublic();
            setpublic();
            target(SiteAddDo);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            set("refalse", 1);
            set("tab", "true");
            var lng = DefaultLng;
            set("lng", lng);
        }

        [HttpPost]
        public virtual void SiteAddDo()
        {
            site s = new site();
            s.pid = 50;
            s.sitename = ctx.Post("sitename");
            s.sitehttp = ctx.Post("sitehttp");
            s.sitedir = ctx.Post("sitedir");
            s.sitecode = ctx.Post("sitecode");
            s.siteip = ctx.Post("siteip");
            s.adminusername = ctx.Post("adminusername");
            s.addtime = DateTime.Now;
            Result result = s.insert();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("sitemain_add_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("sitemain_add_log") + lang("log_extra_ok"), "{Id:" + s.Id + ", Name:'" + s.sitename + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void SiteDel()
        {
            string Ids = ctx.PostIdList("selectinfoid");
            string condition = string.Format("Id in ({0})", Ids);
            if (db.deleteBatch<site>(condition) > 0)
            {
                logService.Add((User)ctx.viewer.obj, lang("sitemain_del_log") + lang("log_extra_ok"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("sitemain_del_log") + lang("log_extra_no"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void SiteSort()
        {
            var infoid = ctx.PostIdList("infoid");
            var pid = ctx.PostIdList("pid");
            var strArray = infoid.Split(new char[] { ',' });
            var strArray1 = pid.Split(new char[] { ',' });
            for (int i = 0; i < strArray.Length; i++)
            {
                string condition = string.Format("Id = {0}", strArray[i]);
                db.updateBatch<site>("set pid=" + strArray1[i], condition);
            }
            logService.Add((User)ctx.viewer.obj, lang("sitemain_log_sort") + lang("log_extra_ok"), "{Id:" + infoid + "'}", "", ctx.Ip);
            ctx.web.ResponseWrite("true");
            ctx.web.ResponseEnd();
        }

    }
}
